from django.shortcuts import render
from django.views import View

from apps.contents.models import GoodsChannel, ContentCategory

# Create your views here.


class IndexView(View):
    """定义首页测试视图"""
    def get(self, request):
        categories = {}
        # channels = GoodsChannel.objects.all().order_by('group_id', 'sequence'),当对查出来的所有数据进行排序时，all()可以省略
        channels = GoodsChannel.objects.order_by('group_id', 'sequence')
        for channel in channels:
            group_id = channel.group.id
            if group_id not in categories:
                categories[group_id] = {"channels": [], "sub_cats": []}
            cat1 = channel.category
            categories[group_id]['channels'].append({
                "id": channel.id,
                "name": cat1.name,
                "url": channel.url
            })
            for cat2 in cat1.subs.all():
                sub_cats = []
                for cat3 in cat2.subs.all():
                    sub_cats.append({
                        "id": cat3.id,
                        "name": cat3.name
                    })
                categories[group_id]['sub_cats'].append({
                    "id": cat2.id,
                    "name": cat2.name,
                    "sub_cats": sub_cats
                })

        contents = {}
        content_categories = ContentCategory.objects.all()
        for content_cat in content_categories:
            contents[content_cat] = content_cat.content_set.filter(status=True).order_by('sequence')
        pass


